//
//  InformationListViewController.m
//  gamebox_qpp
//
//  Created by 赵隆杰 on 2022/1/6.
//

#import "InformationListViewController.h"
#import "InformationListApi.h"
#import "InformationModel.h"
#import "InformationTableViewCell.h"
#import "InformationDetailViewController.h"

#import "InformationTableView.h"

@interface InformationListViewController ()

@property (nonatomic, strong) InformationTableView *storeTableView;

@property (nonatomic, strong) NSNumber *typeId;

@property (nonatomic, strong) NSMutableArray *dataSources;

@property (nonatomic, strong) InformationListApi *listApi;

@end

@implementation InformationListViewController

- (InformationListApi *)listApi
{
    if (!_listApi) {
        _listApi = [[InformationListApi alloc] initWithType:self.typeId];
        _listApi.pageSize = 10;
    }
    return _listApi;
}

- (NSMutableArray *)dataSources
{
    if (!_dataSources) {
        _dataSources = [NSMutableArray array];
    }
    return _dataSources;
}

- (instancetype)initWithType:(NSNumber *)typeId
{
    self = [super init];
    if (self) {
        self.typeId = typeId;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.naviView.hidden = YES;
    
    InformationTableView *storeTableView = [[InformationTableView alloc] init];
    [self.view addSubview:storeTableView];
    [storeTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    VHWeakSelf
    storeTableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        VHStrongSelf
        self.listApi.pageNum ++;
        [self refreshHandler:^{
            
        }];
    }];
//    storeTableView.tableHeaderReloadBlock = ^{
//       VHStrongSelf
//        [self refreshData];
//    };
    storeTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        VHStrongSelf
        [self refreshData];
    }];
    self.storeTableView = storeTableView;
    [self refreshData];
}

- (void)refreshData
{
    self.listApi.pageNum = 1;
    [self refreshHandler:^{
        [self.storeTableView tableHeaderRefreshFinish];
    }];
}


-(void)refreshHandler:(void(^)(void))block
{
    [self.listApi startWithCompletionBlockWithSuccess:^(__kindof YTKBaseRequest * _Nonnull request) {
        if(self.listApi.success){
            if (self.listApi.pageNum == 1) {
                [self.dataSources removeAllObjects];
            }
            NSArray *dataArray = [InformationModel mj_objectArrayWithKeyValuesArray:self.listApi.resArr];
            [self.dataSources addObjectsFromArray:dataArray];
            self.storeTableView.modelList = self.dataSources;
        }
        [self tableViewEndRefresh:self.listApi.isMore];
        block();
    } failure:^(__kindof YTKBaseRequest * _Nonnull request) {
        [self tableViewEndRefresh:self.listApi.isMore];
        block();
    }];
}

- (void)tableViewEndRefresh:(BOOL)isMore
{
    if (isMore) {
        [self.storeTableView.mj_footer endRefreshing];
    }else{
        [self.storeTableView.mj_footer endRefreshingWithNoMoreData];
    }
    [self.storeTableView.mj_header endRefreshing];
}








@end
